package jduck.ui;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.InputEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class Clicker extends Thread {
	JButton button = null;
	JLabel label = null;
	JFrame frame = null;
	long interval = 0L;
	long time = 0;
	long ranadd = 0;
	Script script;
	Random ran = new Random();

	public Clicker(JButton button, JLabel label, JFrame frame, long interval, long time, long ranadd, Script script) {
		this.button = button;
		this.label = label;
		this.frame = frame;
		this.interval = interval;
		this.time = time;
		this.ranadd = ranadd;
		this.script = script;
	}

	public void run() {
		String currButtonText = button.getText();
		button.setText("Stop");
		DateFormat doneft = new SimpleDateFormat("MM/dd hh:mm:ss aa");
		String currtitle = frame.getTitle();
		frame.setTitle("....");
		// label.setText("<html>Starting<br>clicker</html>");
		frame.setIconImage(Toolkit.getDefaultToolkit().getImage(jDuck.class.getResource("/jduck/ui/running.png")));
		boolean keepRunning = true;
		boolean stoppedEarly = false;
		long start = System.currentTimeMillis();
		try {
			Robot robot = new Robot();
			long click = 0;
			long stoptime = start + time;
			long lastclicktime = System.currentTimeMillis();
			long nextclick = lastclicktime + interval +  (long)(ran.nextInt((int)ranadd));
			frame.setTitle("Running...");
			while (keepRunning) {
				long now = System.currentTimeMillis();
				long nowAfterRun = 0L;

				if (now >= nextclick ) {
					click++;
					label.setText("<html>Run: " + (longSpanToStringShort(now - start)) + "/"
							+ longSpanToStringShort(time) + "  " + click + " runs<br> running now! </html>");
					frame.setTitle("+++");
					script.execute(start, stoptime, label);
					lastclicktime = System.currentTimeMillis();
					nextclick = System.currentTimeMillis() + interval + (long)(ran.nextInt((int)ranadd));
				} else {
					frame.setTitle("Running...");
					label.setText("<html>Run: " + (longSpanToStringShort(now - start)) + "/"
							+ longSpanToStringShort(time) + "  " + click + " runs<br> Time to next click: "
							+ (longSpanToStringShort(nextclick - now)) + "</html>");
				}

				if (stoptime != now && now > stoptime) // either we set a count
														// of zero
					keepRunning = false;
				else
					Thread.sleep(1000L);
			}
		} catch (AWTException e) {
			JOptionPane.showMessageDialog(null, "Exception during  thread: " + e.getMessage(), "Thread Exception",
					JOptionPane.INFORMATION_MESSAGE);
		} catch (InterruptedException e) {
			stoppedEarly = true;
		}
		button.setText(currButtonText);
		long finishTime = System.currentTimeMillis();
		if (stoppedEarly)
			label.setText("<html>Ran for: " + longSpanToStringShort(finishTime - start) + " <br>Stopped at: "
					+ doneft.format(new Date()) + "</html>");
		else
			label.setText("<html>Ran for: " + longSpanToStringShort(finishTime - start) + " <br>Completed at: "
					+ doneft.format(new Date()) + "</html>");

		frame.setIconImage(Toolkit.getDefaultToolkit().getImage(jDuck.class.getResource("/jduck/ui/main.png")));

		frame.setTitle(currtitle);

	}

	private static final long second = 1000L;
	// private static final double secondD = 1000D;

	// private static final long ms_second/1L;
	// private static final double msD = secondD/1D;
	//
	// private static final long us=second/1000L;
	// private static final double usD = secondD/1000D;

	private static final long minute = second * 60L;
	private static final long hour = minute * 60L;
	private static final long day = hour * 24L;

	public static String longSpanToStringShort(long time) {
		long d = 0L, h = 0L, m = 0L, s = 0L, millisec = 0L;

		d = time / day;
		h = (time % day) / hour;
		m = (time % hour) / minute;
		s = (time % minute) / second;

		millisec = time % second;

		int precision = 2;

		StringBuilder b = new StringBuilder(12);
		if (d != 0 && precision > 0) {
			b.append(d).append("d");
			precision--;
		}
		if (h != 0 && precision > 0) {
			b.append(h).append("h");
			precision--;
		}
		if (m != 0 && precision > 0) {
			b.append(m).append("m");
			precision--;
		}
		if (s != 0 && precision > 0) {
			b.append(s).append("s");
			precision--;
		}
		if (millisec != 0 && precision > 0 && time < 1000L) {
			b.append(millisec).append("ms");
			precision--;
		}
		return b.toString();
	}

}
